﻿using e_commerce.BLL.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace e_commerce.BLL.Services.Enterprise
{
    public class AdminService : IUserEnterpriseManager,IDisposable
    {
        private commerceDbContext _dbContext;
        public AdminService()
        {
            _dbContext = new commerceDbContext();

        }
        public void CreateEmployee(string employeeName)
        {
            User user = CreateUser(employeeName);
            Employee employee = new Employee()
            {
                UserId = user.UserId,
            };
            _dbContext.Employee.Add(employee);
            SaveChanges();
        }

        public User CreateUser(string userName)
        {
            User user = new User()
            {
                Account = userName,
                NickName = userName,
                Password = userName,
                CreateTime = DateTime.Now,
                PhoneNumber = string.Empty,
                Email = string.Empty,
            };
            _dbContext.User.Add(user);
            SaveChanges();
            return user;
            
        }
        //暂时放置
        public void CreateRole(string roleName, int[] permissionids)
        {
            Role role = new Role()
            {
                RoleName = roleName,
                CreationTime = DateTime.Now,
            };
        }

        public void DeleteEmployeeByEmployeeId(int employeeId)
        {
            _dbContext.Employee.Remove(_dbContext.Employee.SingleOrDefault(e => e.Id == employeeId));
        }

        public void DeleteRoleByRoleId(int roleId)
        {
            _dbContext.Role.Remove(_dbContext.Role.SingleOrDefault(r => r.RoleId == roleId));
        }

        public void ModifyRoleByRoleId(int roleId, int[] permisssionids, string roleName)
        {
            var role = _dbContext.Role.Include("permisssions").SingleOrDefault(r => r.RoleId == roleId);        
            role.Permissions = GetPermissionsByIds(permisssionids);
            SaveChanges();
        }

        public List<Permissions> GetPermissionsByIds(int[] permissionsId)
        {
            var permissions = _dbContext.Permissions;
            List<Permissions> newPermissions = new List<Permissions>();
            foreach (var pid in permissionsId)
            {
                newPermissions.Add(permissions.SingleOrDefault(p => pid == p.PermissionsId));
            }
            return newPermissions;
        }

        public List<Role> GetRoleByIds(int[] roleIds)
        {
            var role = _dbContext.Role;
            List<Role> newRole = new List<Role>();
            foreach (var pid in roleIds)
            {
                newRole.Add(role.SingleOrDefault(p => pid == p.RoleId));
            }
            return newRole;
        }

        public void SetPermissionsToUser(int userId, int[] permissionId)
        {
            var user = _dbContext.User.Include("Permissions").SingleOrDefault(u => u.UserId == userId);
            user.Permissions = GetPermissionsByIds(permissionId);
            SaveChanges();
        }

        public void SetRolesToUser(int userId, int[] roleId)
        {
            var user = _dbContext.User.Include("Permissions").SingleOrDefault(u => u.UserId == userId);
            user.Role = GetRoleByIds(roleId);
            SaveChanges();
        }

        public void SaveChanges()
        {
            _dbContext.SaveChanges();
        }
        public void Dispose()
        {
            _dbContext.Dispose();
        }
    }
}
